package com.compomics.proteocloud.ui;

import com.compomics.proteocloud.starter.CloudTools;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Represents the progress dialog with the property change listener support.
 *
 * @author Thilo Muth
 */
public class ProgressDialog extends JDialog implements PropertyChangeListener {
    /**
     * CloudTools instance.
     */
    public CloudTools cloudTools;

    /**
     * The progress bar displaying the process progress.
     */
    private JProgressBar progressBar = new JProgressBar();

    /**
     * ProgressDialog instance.
     */
    private ProgressDialog instance;

    private int progress;
    private int maximum;

    /**
     * Constructs the ProgressDialog
     * @param mainFrame ProteoCloudUI mainFrame
     * @param title Title of the dialog.
     * @param modal Modal state.
     */
    public ProgressDialog(CloudTools cloudTools, ProteoCloudUI mainFrame, String title, boolean modal) {
        super(mainFrame, title, modal);
        this.cloudTools = cloudTools;
        this.instance = this;
        progressBar.setStringPainted(true);
        setLayout(new BorderLayout());
        JPanel progPanel = new JPanel();
        progPanel.setMinimumSize(new Dimension(500, 80));
        progPanel.setPreferredSize(new Dimension(500, 80));
        progPanel.setLayout(new BoxLayout(progPanel, BoxLayout.X_AXIS));
        progPanel.add(Box.createHorizontalStrut(15));
        progPanel.add(progressBar);
        progPanel.add(Box.createHorizontalStrut(15));
        add(progPanel);
        pack();
        setLocationRelativeTo(mainFrame);
        cloudTools.addPropertyChangeListener(instance);
        setVisible(true);
    };

    public void propertyChange(PropertyChangeEvent evt) {
        String name = evt.getPropertyName();
        if (name.equalsIgnoreCase("new")) {
            progress = 0;
            maximum = (Integer) evt.getNewValue();
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    progressBar.setValue(0);
                    progressBar.setMaximum(maximum);
                }
            });
        } else if (name.equalsIgnoreCase("indeterminate")) {
            final boolean indeterminate = (Boolean) evt.getNewValue();
            if(indeterminate) progressBar.setStringPainted(false);
            progressBar.setIndeterminate(indeterminate);
        } else if (name.equalsIgnoreCase("progressmade")) {
            progress++;
        } else if (name.equalsIgnoreCase("progress")) {
            progress = (Integer) evt.getNewValue();
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    progressBar.setValue(progress);
                }
            });

        } else if (name.equalsIgnoreCase("new message")) {
            instance.setTitle(evt.getNewValue().toString());
        } else if(name.equalsIgnoreCase("close")) {
            instance.dispose();
        }
    }
}
